#include "preferenceswindow.h"
#include <QDebug>
#include <QPalette>
#include <QSettings>

PreferencesWindow::PreferencesWindow(QWidget *mainWindow) :
    QDialog(0),
    applyButton(this)
{

    QSettings settings;
    serverIP = settings.value("serverAddress",QVariant("127.0.0.1")).toString();
    serverPass = settings.value("serverPassword",QVariant("lagpass")).toString();
    userName = settings.value("userName",QVariant("Hatchling")).toString();
    userPass = settings.value("userPassword",QVariant("fakePass")).toString();
    serverPort = settings.value("serverPort",QVariant(32242)).toInt();
    clientPort = settings.value("clientPort",QVariant(32243)).toInt();
    appPort = settings.value("appPort",QVariant(32244)).toInt();

    //setFixedSize(375, 200);
    setWindowTitle("OSCthulhu Options");
    setAcceptDrops(false);
    setWindowFlags(Qt::WindowStaysOnTopHint);
    //setWindowOpacity(0.9);

    QLabel* uN = new QLabel("User Name");
    QLabel* uP = new QLabel("User Password");
    QLabel* sA = new QLabel("Server Address");
    QLabel* sPass = new QLabel("Server Password");
    QLabel* sPort = new QLabel("Server Port");
    QLabel* cP = new QLabel("Client Port");
    QLabel* aP = new QLabel("App Port");

    QPalette palette = this->palette();
    palette.setColor(QPalette::WindowText,QColor(0,0,0));
    palette.setColor(QPalette::ButtonText,QColor(0,0,0));
    palette.setColor(QPalette::Text,QColor(0,0,0));
    uN->setStyleSheet("QLabel { color : black; }");
    uP->setStyleSheet("QLabel { color : black; }");
    sA->setStyleSheet("QLabel { color : black; }");
    sPass->setStyleSheet("QLabel { color : black; }");
    sPort->setStyleSheet("QLabel { color : black; }");
    cP->setStyleSheet("QLabel { color : black; }");
    aP->setStyleSheet("QLabel { color : black; }");

    this->setPalette(palette);

    userNameEntry.setText(userName);
    layout.addRow(uN,&userNameEntry);

    userPassEntry.setText(userPass);
    layout.addRow(uP,&userPassEntry);

    serverIPEntry.setText(serverIP);
    layout.addRow(sA,&serverIPEntry);

    serverPassEntry.setText(serverPass);
    layout.addRow(sPass,&serverPassEntry);

    serverPortEntry.setMinimum(1);
    serverPortEntry.setMaximum(50000);
    serverPortEntry.setValue(serverPort);
    layout.addRow(sPort,&serverPortEntry);

    clientPortEntry.setMinimum(1);
    clientPortEntry.setMaximum(50000);
    clientPortEntry.setValue(clientPort);
    layout.addRow(cP,&clientPortEntry);

    appPortEntry.setMinimum(1);
    appPortEntry.setMaximum(50000);
    appPortEntry.setValue(appPort);
    layout.addRow(aP,&appPortEntry);

    applyButton.setText("Apply");
    QObject::connect(&applyButton,SIGNAL(clicked()),this,SLOT(applied()));
    layout.addWidget(&applyButton);

    setLayout(&layout);
}

void PreferencesWindow::applied()
{
    hide();
    userName = userNameEntry.text();
    userPass = userNameEntry.text();
    serverIP = serverIPEntry.text();
    serverPass = serverPassEntry.text();
    serverPort = serverPortEntry.value();
    clientPort = clientPortEntry.value();
    appPort = appPortEntry.value();

    emit apply(
                userNameEntry.text(),
                userPassEntry.text(),
                serverIPEntry.text(),
                serverPassEntry.text(),
                serverPortEntry.value(),
                clientPortEntry.value(),
                appPortEntry.value()
    );
    qDebug() << "applied";
}
